<?php
namespace app\admin\controller;
use think\Request;
use \think\Session;
use \think\Cookie;
use \user\Auth;

class Index
{
    public function index()
    {
        Cookie::init(['prefix'=>'admin_','path'=>'/','httponly'=>true]);
        if(!Session::has('id') || !Session::has('username')){
            return view('login');
        }else if (!Cookie::has('token')){
            Session::delete('id');
            Session::delete('username');
            Session::delete('username_dsp');
            return view('login');
        }else{
            $token = Cookie::get('token');
            $check_res = Auth::check($token);
            if($check_res['resCode'] == 2){
                return view('index',[
                    'username'  => Session::get('username'),
                    'username_dsp' => Session::get('username_dsp')
                ]);
            }else{
                Session::delete('id');
                Session::delete('username');
                Session::delete('username_dsp');
                return view('login');
            }
        }
    }

    public function test()
    {
        $request = Request::instance();
        $password = $request->get('ps', '', 'safe_replace');
        echo $password;exit;
        $arr = array(
            'function' => Auth::AUTH_MODULE . ".Authority." . "addRole",
            'data' => array(
                'title' => '超级管理员'
            )
        );
        $param = array(
            'data' => json_encode($arr)
        );
        $result = Auth::makeRequest(Auth::AUTH_URL, $param);
        var_dump($result);
    }
}
